<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/3/29
 * Time: 16:23
 */

namespace App\Http\Controllers\VipChannel\Fans;

use App\Http\Controllers\VipChannel\BaseController;
use App\Http\Controllers\VipChannel\Fans\Transformers\FansDataInfoTransformer;
use App\Modules\Channel\Models\Channel;
use App\Modules\Channel\Models\ChannelUser;
use App\Modules\Util;
use Illuminate\Http\Request;

class FansController extends BaseController
{
    public function getDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        $channels = $this->getDistributionChannelIds($request);

        if (count($channels) <= 0) {
            return response()->error('NOT_EXIST');
        }
        $params = ['distribution_channel_ids' => $channels, 'groupBy' => 'date', 'date' => $date, 'start_date' => $start_date, 'end_date' => $end_date];
        $result = Channel::getChannelFansData($params, false);
        foreach ($result as $item) {
            $item->thirty_days_recharge_arpu = round(Util::getPersentAmountInteger($item->thirty_days_recharge, $item->new_subscribe_num), 2);
            $item->sixty_days_recharge_arpu = round(Util::getPersentAmountInteger($item->sixty_days_recharge, $item->new_subscribe_num), 2);
            $item->new_sub_user_arpu = round(Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->new_subscribe_num), 2);
            $item->actual_subscribe_user_arpu = round(Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->actual_subscribe_num), 2);
            $new_sub_pay_rate = round(Util::getPersentAmountInteger($item->new_sub_recharge_num, $item->new_subscribe_num), 2);
            $item->new_sub_pay_rate = round($new_sub_pay_rate * 100, 2) . '%';
        }
        return response()->pagination(new FansDataInfoTransformer(), $result);
    }

    public function exportDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        $channels = $this->getDistributionChannelIds($request);

        if (count($channels) <= 0) {
            return response()->error('NOT_EXIST');
        }
        $params = ['distribution_channel_ids' => $channels, 'groupBy' => 'date', 'date' => $date, 'start_date' => $start_date, 'end_date' => $end_date];
        $result = Channel::getChannelFansData($params, true);
        $header = ['日期', '新关粉丝数', '24小时充值', '72小时充值', '7天充值', '30天充值', '30天arpu值', '60天充值', '60天arpu值',
            '新关arpu值', '净关arpu值', '新关粉丝累计充值', '付费笔数', '新关付费人数', '新关付费率'];
        $data = [];
        foreach ($result as $item) {
            $item->thirty_days_recharge_arpu = round(Util::getPersentAmountInteger($item->thirty_days_recharge, $item->new_subscribe_num), 2);
            $item->sixty_days_recharge_arpu = round(Util::getPersentAmountInteger($item->sixty_days_recharge, $item->new_subscribe_num), 2);
            $item->new_sub_user_arpu = round(Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->new_subscribe_num), 2);
            $item->actual_subscribe_user_arpu = round(Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->actual_subscribe_num), 2);
            $new_sub_pay_rate = round(Util::getPersentAmountInteger($item->new_sub_recharge_num, $item->new_subscribe_num), 2);
            $item->new_sub_pay_rate = round($new_sub_pay_rate * 100, 2) . '%';
            $data[] = array(
                $item->date,
                $item->new_subscribe_num,
                $item->one_day_recharge,
                $item->three_days_recharge,
                $item->seven_days_recharge,
                $item->thirty_days_recharge,
                $item->thirty_days_recharge_arpu,
                $item->sixty_days_recharge,
                $item->sixty_days_recharge_arpu,
                $item->new_sub_user_recharge_amount,
                $item->new_sub_user_arpu,
                $item->actual_subscribe_user_arpu,
                $item->sub_user_orders_num,
                $item->new_sub_recharge_num,
                $item->new_sub_pay_rate
            );
        }
        saveExcelData($header, $data, '', '', '粉丝数据');
    }

    public function getCatalogDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $type = $request->has('type') ? $request->input('type') : 'site';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        //此处 channel_user_id 其实为 phone
        $phone = $request->has('channel_user_id') ? $request->input('channel_user_id') : '';
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if (!in_array($type, ['site', 'account'])) {
            return response()->error('PARAM_ERROR');
        }

        $groupBy = $type;
        $distribution_channel_ids = $this->getDistributionChannelIds($request);

        if ($phone) {
            \Log::info('getByPhone');
            $channel_user = ChannelUser::getByPhone($phone);
            if ($channel_user) {
                $distribution_channel_ids = ChannelUser::getChannelIdArray($channel_user->id);
            }
        }

        if ($distribution_channel_id) {
            $distribution_channel_ids = explode(',', $distribution_channel_id);
        }

        if (count($distribution_channel_ids) <= 0) {
            return response()->error('NOT_EXIST');
        }

        $params = compact('distribution_channel_ids', 'date', 'groupBy', 'start_date', 'end_date');
        $result = Channel::getChannelFansData($params, false);

        $isSiteType = ($type == 'site');
        foreach ($result as $item) {
            if ($isSiteType) {
                $item->distribution_channel_id = $item->distribution_channel_id . '(' . $item->site_nick_name . ')';
            } else {
                $item->phone = $item->phone . '(' . $item->channel_user_nickname . ')';
            }
            $item->thirty_days_recharge_arpu = round(Util::getPersentAmountInteger($item->thirty_days_recharge, $item->new_subscribe_num), 2);
            $item->sixty_days_recharge_arpu = round(Util::getPersentAmountInteger($item->sixty_days_recharge, $item->new_subscribe_num), 2);
            $item->new_sub_user_arpu = round(Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->new_subscribe_num), 2);
            $item->actual_subscribe_user_arpu = round(Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->actual_subscribe_num), 2);
            $new_sub_pay_rate = round(Util::getPersentAmountInteger($item->new_sub_recharge_num, $item->new_subscribe_num), 2);
            $item->new_sub_pay_rate = round($new_sub_pay_rate * 100, 2) . '%';
        }
        return response()->pagination(new FansDataInfoTransformer(), $result);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function exportCatalogDataInfo(Request $request)
    {
        $date = $request->has('date') ? $request->input('date') : '';
        $type = $request->has('type') ? $request->input('type') : 'site';
        $end_date = $request->has('end_date') ? $request->input('end_date') : '';
        $start_date = $request->has('start_date') ? $request->input('start_date') : '';
        //此处 channel_user_id 其实为 phone
        $phone = $request->has('channel_user_id') ? $request->input('channel_user_id') : '';
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if (!in_array($type, ['site', 'account'])) {
            return response()->error('PARAM_ERROR');
        }

        $groupBy = $type;
        $distribution_channel_ids = $this->getDistributionChannelIds($request);

        if ($phone) {
            $channel_user = ChannelUser::getByPhone($phone);
            if ($channel_user) {
                $distribution_channel_ids = ChannelUser::getChannelIdArray($channel_user->id);
            }
        }

        if ($distribution_channel_id) {
            $distribution_channel_ids = explode(',', $distribution_channel_id);
        }

        if (count($distribution_channel_ids) <= 0) {
            $result = [];
        } else {
            $params = compact('distribution_channel_ids', 'date', 'groupBy', 'start_date', 'end_date');
            $result = Channel::getChannelFansData($params, true);
        }

        $header = ['日期', ($type == 'account') ? '账号' : '站点', '名称', '新关粉丝数', '24小时充值', '72小时充值', '7天充值', '30天充值', '30天arpu值', '60天充值', '60天arpu值',
            '新关arpu值', '净关arpu值', '新关粉丝累计充值', '付费笔数', '新关付费人数', '新关付费率'];
        $data = [];
        foreach ($result as $item) {
            $item->thirty_days_recharge_arpu = Util::getPersentAmountInteger($item->thirty_days_recharge, $item->new_subscribe_num);
            $item->sixty_days_recharge_arpu = Util::getPersentAmountInteger($item->sixty_days_recharge, $item->new_subscribe_num);
            $item->new_sub_user_arpu = Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->new_subscribe_num);
            $item->actual_subscribe_user_arpu = Util::getPersentAmountInteger($item->new_sub_user_recharge_amount, $item->actual_subscribe_num);
            $item->new_sub_pay_rate = Util::getPersentAmountInteger($item->new_sub_recharge_num, $item->new_subscribe_num);
            $data[] = array(
                $item->date,
                ($type == 'account') ? $item->phone : $item->distribution_channel_id,
                ($type == 'account') ? $item->channel_user_nickname : $item->site_nick_name,
                $item->new_subscribe_num,
                $item->one_day_recharge,
                $item->three_days_recharge,
                $item->seven_days_recharge,
                $item->thirty_days_recharge,
                $item->thirty_days_recharge_arpu,
                $item->sixty_days_recharge,
                $item->sixty_days_recharge_arpu,
                $item->new_sub_user_recharge_amount,
                $item->new_sub_user_arpu,
                $item->actual_subscribe_user_arpu,
                $item->sub_user_orders_num,
                $item->new_sub_recharge_num,
                $item->new_sub_pay_rate
            );
        }
        saveExcelData($header, $data, '', '', '粉丝数据');
    }
}